package com.luge.heart;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.luge.base.Base;
import com.luge.base.BaseActivity;
import com.luge.model.share.SinaOAuth;
import com.luge.utils.FileManager;
import com.luge.utils.Helper;

/**
 * Type,ShareUrl
 */
public class ShareActivity extends BaseActivity implements OnClickListener {
	private static final int REQUEST_CODE = 0;
	private static final int REQUEST_LOCAL_CODE = 1;
	private static final int REQUEST_CODE_RESULT = 9;
	private static final String IMAGE_UNSPECIFIED = "image/*";

	private TextView titleTxt;
	private EditText shareEdit;
	private ImageView shareImage;
	private Button addPhoto;
	private Button shareBtn;

	private SinaOAuth OAuth;
	private String picPath;
	private String shareUrl;
	private String shareStr;

	@Override
	protected void main() {
		OAuth = new SinaOAuth();
		OAuth.instantiate(ShareActivity.this);
	}

	@Override
	protected void init() {
		shareUrl = getIntent().getStringExtra("ShareUrl");

		setContentView(R.layout.share_layout);
		titleTxt = (TextView) findViewById(R.id.share_title);
		titleTxt.setText(getString(R.string.share_to_str)
				+ getString(R.string.sina_str));
		findViewById(R.id.share_back).setOnClickListener(this);
		shareEdit = (EditText) findViewById(R.id.share_edit);
		shareBtn = (Button) findViewById(R.id.share_layout_share_btn);
		shareBtn.setOnClickListener(this);
		addPhoto = (Button) findViewById(R.id.share_add_photo);
		addPhoto.setOnClickListener(this);
		shareImage = (ImageView) findViewById(R.id.share_image);
	}

	@Override
	public void onClick(View v) {
		int viewId = v.getId();
		switch (viewId) {
		case R.id.share_layout_share_btn:
			shareStr = shareEdit.getText().toString();
			if (TextUtils.isEmpty(shareStr)) {
				Helper.toast(ShareActivity.this, R.string.feedback_content_str);
			} else {
				shareStr = new StringBuilder().append(shareStr)
						.append(shareUrl).toString();
				OAuth.shareTwitter(ShareActivity.this, shareStr, picPath);
			}
			break;
		case R.id.share_add_photo:
			showPhotoSelectDialog();
			break;
		case R.id.share_back:
			finish();
			break;
		default:
			break;
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case REQUEST_CODE:
			if (data != null && data.getData() != null) {
				startPhotoZoom(data.getData());
			}
			break;
		case REQUEST_LOCAL_CODE:
			File file = new File(picPath);
			if (file.length() > 0) {
				startPhotoZoom(Uri.fromFile(file));
			}
			break;
		case REQUEST_CODE_RESULT:
			if (data != null) {
				try {
					Bitmap bitmap = BitmapFactory.decodeFile(picPath);
					shareImage.setImageBitmap(bitmap);
				} catch (OutOfMemoryError e) {

				}
			} else {
				FileManager.getInstance().deleteFile(picPath);
			}
			break;
		default:
			break;
		}
	}

	private File getTempFile() {
		File tempFile = new File(new StringBuilder(Base.photo_path)
				.append(new SimpleDateFormat("yyyyMMdd_hhmmss")
						.format(new Date(System.currentTimeMillis())))
				.append(".jpg").toString());
		try {
			FileManager.getInstance().creatDir(Base.photo_path);
			tempFile.createNewFile();
			picPath = tempFile.getAbsolutePath();
			return tempFile;
		} catch (IOException e) {
			e.printStackTrace();
			Helper.toast(this, getString(R.string.creat_file_fail));
		}
		return null;
	}

	private void startPhotoZoom(Uri uri) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
		intent.putExtra("crop", "true");
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		intent.putExtra("outputX", 320);
		intent.putExtra("outputY", 320);
		intent.putExtra("return-data", false);
		intent.putExtra("onFaceDetection", true);
		intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile()));
		startActivityForResult(intent, REQUEST_CODE_RESULT);
	}

	private void showPhotoSelectDialog() {
		new AlertDialog.Builder(this)
				.setTitle(R.string.photo_select)
				.setItems(R.array.photo_select,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								Intent mIntent = new Intent();
								switch (which) {
								case 0:
									mIntent.setAction("android.media.action.IMAGE_CAPTURE");
									mIntent.putExtra(MediaStore.EXTRA_OUTPUT,
											Uri.fromFile(getTempFile()));
									startActivityForResult(mIntent,
											REQUEST_LOCAL_CODE);
									break;
								case 1:
									mIntent.setAction(Intent.ACTION_GET_CONTENT);
									mIntent.setType(IMAGE_UNSPECIFIED);
									startActivityForResult(mIntent,
											REQUEST_CODE);
									break;
								case 2:
									break;
								}
							}
						}).show();
	}
}
